<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>JS中的正则表达式 | 黑不溜球de博客么么哒</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="正则表达式 的创建参数: “正则表达式”,”匹配模式”(i和g)i表示忽略大小写  g表示全局匹配    1234567var str1 &#x3D; &quot;bababadadaccccvbg&quot;var reg &#x3D; new RegExp(&quot;a&quot;,&quot;i&quot;);&#x2F;*用test()来检验正则表达式所约束的内容*&#x2F;conso">
<meta property="og:type" content="article">
<meta property="og:title" content="JS中的正则表达式">
<meta property="og:url" content="http://example.com/2020/11/18/zhengze/index.html">
<meta property="og:site_name" content="黑不溜球de博客么么哒">
<meta property="og:description" content="正则表达式 的创建参数: “正则表达式”,”匹配模式”(i和g)i表示忽略大小写  g表示全局匹配    1234567var str1 &#x3D; &quot;bababadadaccccvbg&quot;var reg &#x3D; new RegExp(&quot;a&quot;,&quot;i&quot;);&#x2F;*用test()来检验正则表达式所约束的内容*&#x2F;conso">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-11-18T00:08:03.213Z">
<meta property="article:modified_time" content="2020-11-18T00:23:09.917Z">
<meta property="article:author" content="Jone-xin">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/my_blog/atom.xml" title="黑不溜球de博客么么哒" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/my_blog/css/style.css">

<meta name="generator" content="Hexo 5.2.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/my_blog/" id="logo">黑不溜球de博客么么哒</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/my_blog/">主页</a>
        
          <a class="main-nav-link" href="/my_blog/archives">归档</a>
        
          <a class="main-nav-link" target="_blank" rel="noopener" href="https://gitee.com/xiangxinag-darling">@我的gitee</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/my_blog/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-zhengze" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/my_blog/2020/11/18/zhengze/" class="article-date">
  <time datetime="2020-11-18T00:08:03.213Z" itemprop="datePublished">2020-11-18</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      JS中的正则表达式
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h4 id="正则表达式-的创建"><a href="#正则表达式-的创建" class="headerlink" title="正则表达式 的创建"></a>正则表达式 的创建</h4><p>参数: “正则表达式”,”匹配模式”(i和g)i表示忽略大小写  g表示全局匹配   </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">var str1 &#x3D; &quot;bababadadaccccvbg&quot;</span><br><span class="line">var reg &#x3D; new RegExp(&quot;a&quot;,&quot;i&quot;);</span><br><span class="line">&#x2F;*用test()来检验正则表达式所约束的内容*&#x2F;</span><br><span class="line">console.log(reg.test(str1));</span><br><span class="line">&#x2F;*简单创建正则表达式*&#x2F;</span><br><span class="line">let reg &#x3D; &#x2F;ad&#x2F;i;</span><br><span class="line">console.log(reg.test(str1));</span><br></pre></td></tr></table></figure>

<h4 id="常用的匹配规则"><a href="#常用的匹配规则" class="headerlink" title="常用的匹配规则"></a>常用的匹配规则</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">[ab]等价于a|b,表示a或b的关系</span><br><span class="line">[a-z]任意小写字母</span><br><span class="line">[A-Z]任意大写字母</span><br><span class="line">[A-z]任意字母</span><br><span class="line">[0-9]任意数字</span><br><span class="line">[^]除了</span><br><span class="line">量词:</span><br><span class="line">a&#123;3&#125;等价于aaa  </span><br><span class="line">ab&#123;3&#125;等价于abbb </span><br><span class="line">(ab)&#123;3&#125;等价于 ababab</span><br><span class="line">b&#123;1,3&#125; b出现1-3次 等价于  b|bb|bbb</span><br><span class="line">+  相当于&#123;1,&#125;  至少一个</span><br><span class="line">*  相当于&#123;0,&#125;  至少0个</span><br><span class="line">?  相当于&#123;0,1&#125; 0个或者1个</span><br><span class="line">^  表示开头:  ^a 以a开头</span><br><span class="line">$  表示结尾:  b$ 以b结尾</span><br><span class="line">^和$同时使用  表示^$之间必须完全满足</span><br><span class="line">\w</span><br><span class="line">任意字母,数字,下划线</span><br><span class="line">\W  </span><br><span class="line">除了任意字母,数字.下划线</span><br><span class="line"></span><br><span class="line">\d</span><br><span class="line">任意数字</span><br><span class="line">\D</span><br><span class="line"></span><br><span class="line">\s</span><br><span class="line">空格</span><br><span class="line">\S</span><br><span class="line"></span><br><span class="line">\b</span><br><span class="line">单词边界</span><br><span class="line">\B</span><br></pre></td></tr></table></figure>

<h4 id="简单的检验案例（手机号，电子邮箱）"><a href="#简单的检验案例（手机号，电子邮箱）" class="headerlink" title="简单的检验案例（手机号，电子邮箱）"></a>简单的检验案例（手机号，电子邮箱）</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;手机号</span><br><span class="line">let reg&#x3D;&#x2F;^1[3-9][0-9]&#123;9&#125;&#x2F;;</span><br><span class="line">str6&#x3D;&quot;192887678766&quot;</span><br><span class="line">console.log(reg.test(str6));</span><br><span class="line">&#x2F;&#x2F;邮箱</span><br><span class="line">电子邮件格式：</span><br><span class="line">\w&#123;3,&#125;  (\.\w+)*  @  [A-z0-9]+  (\.[A-z]&#123;2-5&#125;)&#123;1,2&#125;</span><br><span class="line">var str8&#x3D;&quot;www.qq@adf.com&quot;;</span><br><span class="line">reg&#x3D;&#x2F;^\w&#123;3,&#125;(\.\w+)*@[A-z0-9]+(\.[A-z]&#123;25&#125;)&#123;1,2&#125;$&#x2F;;</span><br><span class="line">console.log(reg.test(str8));    </span><br></pre></td></tr></table></figure>

<h4 id="正则表达式中的五个匹配函数（两个正则的，三个字付串的）"><a href="#正则表达式中的五个匹配函数（两个正则的，三个字付串的）" class="headerlink" title="正则表达式中的五个匹配函数（两个正则的，三个字付串的）"></a>正则表达式中的五个匹配函数（两个正则的，三个字付串的）</h4><p>1，<code>reg.test(str)</code> —– 多从全局匹配调用，下一次将会再上一次结束索引值位置（lastindex）进行往后匹配</p>
<p>—返回  true /false</p>
<p>2，<code>reg.exec(str)</code> —– 多从全局匹配调用，下一次将会再上一次结束索引值位置（lastindex）进行往后匹配`</p>
<p>—返回   匹配项所组成的数组</p>
<p>3，**<code>str.match(reg)</code>**</p>
<p>–返回    匹配字符串所组成的数组(默认返回匹配第一个组成数组，g开启后返回所有符合条件的组成的数组)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">let str &#x3D; &#39;sada s  d&#39; </span><br><span class="line">let a &#x3D; str.match(&#x2F;\s&#x2F;g) </span><br><span class="line">console.log(a) &#x2F;&#x2F; 【&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;】</span><br></pre></td></tr></table></figure>



<p>4，**<code>str.replace(reg)</code>**</p>
<p>–返回   被指定字符串替换后的新字符串;(默认只替换第一个，g开启后，都替换)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">let str &#x3D; &#39;sada s  d&#39; </span><br><span class="line">let a &#x3D; str.replace(&#x2F;\s&#x2F;g,&#39;---&#39;) </span><br><span class="line">console.log(a)</span><br></pre></td></tr></table></figure>



<p>5,<code>str.search(reg)</code>:</p>
<p>返回值：第一次匹配的下标（索引），(g开启也只匹配第一个)</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">et str &#x3D; &#39;sada s  d&#39;    </span><br><span class="line">let a &#x3D; str.search(&#x2F;\s&#x2F;)    </span><br><span class="line">console.log(a)</span><br></pre></td></tr></table></figure>


      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2020/11/18/zhengze/" data-id="ckhmnwyu100066rx4g59mbbpu" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
  
    <a href="/my_blog/2020/11/03/react/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">React基础知识笔记</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/my_blog/archives/2020/11/">November 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/my_blog/archives/2020/10/">October 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/my_blog/2020/11/18/zhengze/">JS中的正则表达式</a>
          </li>
        
          <li>
            <a href="/my_blog/2020/11/03/react/">React基础知识笔记</a>
          </li>
        
          <li>
            <a href="/my_blog/2020/11/03/vue/">Vue基础知识笔记</a>
          </li>
        
          <li>
            <a href="/my_blog/2020/11/03/async/">JS异步编程的三种方案</a>
          </li>
        
          <li>
            <a href="/my_blog/2020/11/03/computer/">计算机网路笔记</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 Jone-xin<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/my_blog/" class="mobile-nav-link">主页</a>
  
    <a href="/my_blog/archives" class="mobile-nav-link">归档</a>
  
    <a target="_blank" rel="noopener" href="https://gitee.com/xiangxinag-darling" class="mobile-nav-link">@我的gitee</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/my_blog/fancybox/jquery.fancybox.css">

  
<script src="/my_blog/fancybox/jquery.fancybox.pack.js"></script>




<script src="/my_blog/js/script.js"></script>




  </div>
</body>
</html>